<?php
namespace App\Models;

use App\Config\Database;
use PDO;
use PDOException;

/**
 * 黑名单模型
 * @package App\Models
 * @description:
 * 1. 添加黑名单
 * 2. 移除黑名单
 * 3. 检查是否在黑名单中
 * 4. 获取黑名单列表
 */
class BlockedUser {
    private $conn;
    private $table = 'blocked_users';

    public function __construct() {
        $database = new Database();
        $this->conn = $database->getConnection();
    }

    /**
     * 添加用户到黑名单
     * @param int $blockerId 拉黑者ID
     * @param int $blockedId 被拉黑者ID
     * @return bool
     */
    public function blockUser($blockerId, $blockedId) {
        try {
            $query = "INSERT INTO " . $this->table . " 
                      (blocker_id, blocked_id) 
                      VALUES (:blocker_id, :blocked_id)";
            
            $stmt = $this->conn->prepare($query);
            $stmt->bindParam(':blocker_id', $blockerId);
            $stmt->bindParam(':blocked_id', $blockedId);
            
            return $stmt->execute();
        } catch (PDOException $e) {
            // 唯一键冲突表示已存在
            if ($e->getCode() == 23000) {
                return true; // 已存在，视为成功
            }
            error_log("Block user error: " . $e->getMessage());
            return false;
        }
    }

    /**
     * 从黑名单移除用户
     * @param int $blockerId 拉黑者ID
     * @param int $blockedId 被拉黑者ID
     * @return bool
     */
    public function unblockUser($blockerId, $blockedId) {
        try {
            $query = "DELETE FROM " . $this->table . " 
                      WHERE blocker_id = :blocker_id AND blocked_id = :blocked_id";
            
            $stmt = $this->conn->prepare($query);
            $stmt->bindParam(':blocker_id', $blockerId);
            $stmt->bindParam(':blocked_id', $blockedId);
            
            return $stmt->execute();
        } catch (PDOException $e) {
            error_log("Unblock user error: " . $e->getMessage());
            return false;
        }
    }

    /**
     * 检查用户是否在黑名单中
     * @param int $blockerId 拉黑者ID
     * @param int $blockedId 被拉黑者ID
     * @return bool
     */
    public function isBlocked($blockerId, $blockedId) {
        try {
            $query = "SELECT COUNT(*) as count 
                      FROM " . $this->table . " 
                      WHERE blocker_id = :blocker_id AND blocked_id = :blocked_id";
            
            $stmt = $this->conn->prepare($query);
            $stmt->bindParam(':blocker_id', $blockerId);
            $stmt->bindParam(':blocked_id', $blockedId);
            $stmt->execute();
            
            $result = $stmt->fetch(PDO::FETCH_ASSOC);
            return $result['count'] > 0;
        } catch (PDOException $e) {
            error_log("Check blocked user error: " . $e->getMessage());
            return false;
        }
    }

    /**
     * 获取黑名单列表
     * @param int $blockerId 拉黑者ID
     * @param int $page 页码
     * @param int $pageSize 每页数量
     * @return array
     */
    public function getBlockedUsers($blockerId, $page = 1, $pageSize = 20) {
        try {
            $offset = ($page - 1) * $pageSize;
            
            $query = "SELECT bu.*, u.user_id, u.username, u.avatar_url 
                      FROM " . $this->table . " bu
                      JOIN users u ON bu.blocked_id = u.id
                      WHERE bu.blocker_id = :blocker_id
                      ORDER BY bu.created_at DESC
                      LIMIT :offset, :limit";
            
            $stmt = $this->conn->prepare($query);
            $stmt->bindParam(':blocker_id', $blockerId);
            $stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
            $stmt->bindParam(':limit', $pageSize, PDO::PARAM_INT);
            
            $stmt->execute();
            return $stmt->fetchAll(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            error_log("Get blocked users error: " . $e->getMessage());
            return [];
        }
    }
}